<?php

# mvblogs_2-0
# 2006 Oct 06  08:57:51
# session user object

# guest user hash: 'b147085c5bf7126d231a3c8072774084'
# a hash is calculated by concatenating the email and the password and then
# applying a md5 string hash.

# @singleton
class SessionUser
{
    static private $hash;
    static private $user_id;
    static private $email;
    static private $name;
    static private $nickname;
    static private $homepage;
    static private $date_registered;
    static private $date_last_seen;
    static private $active;

    private function __construct() {}
    static public function initialize($userId = 0)
    {
        # if user id is not 0 (guest) and does not exist, throw an exception
        if ($userId > 0 && !UsersModel::existsByUserId($userId)) {
            throw new Exception(
                "user object to set with id '{$userId}' does not exist");

        # if user id is 0 (guest)
        } elseif ($userId == 0) {
            $user = UsersModel::getByEmail('guest@mvblogs.org');
        } else {
            $user = UsersModel::getByUserId($userId);
        }

        SessionUser::$hash    = $user['hash'];
        SessionUser::$user_id = $user['user_id'];
        SessionUser::$email   = $user['email'];
        SessionUser::$name    = $user['name'];
        SessionUser::$nickname = $user['nickname'];
        SessionUser::$homepage = $user['homepage'];
        SessionUser::$date_registered = $user['date_registered'];
        SessionUser::$date_last_seen  = $user['date_last_seen'];
        SessionUser::$active = $user['active'];
    }

    static public function initializeWithHash($hash)
    {
        if (strlen($hash) != 32) {
            throw new Exception(
                "cannot initialize session user with invalid hash length");
        } else {
            $user = UsersModel::getByHash($hash);
        }

        SessionUser::$hash    = $user['hash'];
        SessionUser::$user_id = $user['user_id'];
        SessionUser::$email   = $user['email'];
        SessionUser::$name    = $user['name'];
        SessionUser::$nickname = $user['nickname'];
        SessionUser::$homepage = $user['homepage'];
        SessionUser::$date_registered = $user['date_registered'];
        SessionUser::$date_last_seen  = $user['date_last_seen'];
        SessionUser::$active = $user['active'];
    }

    static public function getUserId() { return SessionUser::$userId; }
    static public function getEmail()  { return SessionUser::$email; }
    static public function getName()   { return SessionUser::$name; }
    static public function getNickname() { return SessionUser::$nickname; }
    static public function getHomepage() { return SessionUser::$homepage; }
    static public function getDateRegistered() { return SessionUser::$date_registered; }
    static public function getDateLastSeen()   { return SessionUser::$date_last_seen; }
    static public function getActive() { return SessionUser::$active; }
    static public function getArray()
    {
        $user['hash']    = SessionUser::$hash;
        $user['user_id'] = SessionUser::$user_id;
        $user['email']   = SessionUser::$email;
        $user['name']    = SessionUser::$name;
        $user['nickname'] = SessionUser::$nickname;
        $user['homepage'] = SessionUser::$homepage;
        $user['date_registered'] = SessionUser::$date_registered;
        $user['date_last_seen']  = SessionUser::$date_last_seen;
        $user['active'] = SessionUser::$active;
        return $user;
    }
}
